<template>
  <div class="search">
    <f-form ref="form" :action="api.topic" :form-key="this.formItem.topicId" :file-ids="fileIds">
      <f-form-item label="发布类型" name="topicType" type="radio" v-if="loaded"  :required="true"  :radioButton="true" :data="api.topic.topicType" :value="formItem.topicType"/>
      <f-form-item label="标题" name="topicTitle" :required="true" :value="formItem.topicTitle"/>
      <f-form-item label="开始日期" dataType="object" type="date" name="beginDate1" :required="true" :value="$moment(formItem.beginDate)" @change="beginDateChange"/>
      <f-form-item label="结束日期" dataType="object" type="date" name="endDate1" :required="true" :value="$moment(formItem.endDate)" @change="endDateChange"/>
      <f-form-item label="内容" name="topicDesc" :required="true" type="editor" :value="formItem.topicDesc"/>
      <f-form-item label="轮播图" type="upload" name="coverImage" :value="formItem.coverImage" :file-max="1" @uploadSuccess="uploadSuccess"/>
    </f-form>
  </div>
</template>
<script>
  export default {
    name: 'TopicForm',
    props: {},
    data() {
      return {
        isAdd: true,//是否是添加
        loaded: false,
        fileIds: [],//上传文件ID
        formItem: this.getFormItem()
      }
    },
    methods: {
      getFormItem() {
        return {
          topicId: '',
          topicType: 1,
          coverImage: '',
          beginDate: this.$moment().format('YYYY-MM-DD'),
          endDate: this.$moment().format('YYYY-MM-DD'),
          topicDesc: '',
          topicTitle: ''
        }
      },
      beginDateChange(date) {
        this.formItem.beginDate = date
      },
      endDateChange(date) {
        this.formItem.endDate = date
      },
      //表单赋值
      setData(data) {
        this.$refs.form.resetFields()
        if (data.topicId) {
          this.$refs.form.getDetails({ topicId: data.topicId }, (data) => {
            this.formItem = data
            this.loaded = true
          })
          this.isAdd = false
        } else {
          this.formItem = this.getFormItem()
          this.loaded = true
        }
      },
      //文件上传回调
      uploadSuccess(fileUrl, files, fileIds, removeId) {
        this.fileIds = fileIds.join(',')
      },
      //提交表单
      handleSubmit(callback) {
        this.$refs.form.handlerSubmit({beginDate: this.formItem.beginDate, endDate: this.formItem.endDate}, callback)
      }
    },
    created() {

    },
    components: {}
  }
</script>

<style scoped lang="less" rel="stylesheet/less">

</style>
